#在排序数组中查找元素的第一个和最后一个位置
#           nums =[5,7,7,8,8,8,8,10]，target = 8
from typing import List
class Solution:
    def searchRange(self, nums: List[int], target: int) -> List[int]:
        def search_R(nums,target):
            left,right=0,len(nums)-1
            while left<=right:
                middle=(left+right)//2
                if nums[middle]==target:
                    if middle==len(nums)-1 or nums[middle+1]>target:
                        return middle
                if nums[middle]<=target:
                    left=middle+1
                else:
                    right=middle-1
            return -1

        def search_L(nums,target):
            left,right=0,len(nums)-1
            while left<=right:
                middle=(left+right)//2
                if nums[middle]==target:
                    if middle==0 or nums[middle-1]<target:
                        return middle
                if nums[middle]<target:
                    left=middle+1
                else:
                    right=middle-1
            return -1

        l=search_L(nums,target)
        r=search_R(nums,target)
        return[l,r]